package com.robooot.business_counter.controller;


import com.robooot.business_counter.exception.BizException;
import com.robooot.business_counter.model.PageHelpModel;
import com.robooot.business_counter.model.PageHelpResult;
import com.robooot.business_counter.model.ResultModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

import java.util.List;

@RestController
public class BaseController {


    protected Logger logger = LoggerFactory.getLogger(getClass());

    protected HttpServletRequest getRequest() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        return request;
    }








    protected ResultModel ok(Object... object) {
        if (object.length > 0) {
            return new ResultModel(0,object[0]);
        }
        return new ResultModel(0,"SUCCESS");
    }

    protected ResultModel ok() { return new ResultModel(0,"SUCCESS"); }

    protected ResultModel ok(Object object,String message) { return new ResultModel(0,message,object); }

    protected ResultModel bad(int code, String message) {
        return new ResultModel(code,message);
    }

    protected ResultModel bad(int code, String message, Object object) {
        if (object != null) {
            return new ResultModel(code,message,object);
        }
        return new ResultModel(code,message);
    }




    /**
     * 自定义解析分页参数
     */

    protected void pageHelper(final Object object, int defaultPageRow){
        if(object instanceof PageHelpModel){
            PageHelpModel baseVO = (PageHelpModel)object;
            Integer pageNum = baseVO.getPageNum()==null?1:baseVO.getPageNum();
            Integer pageRow = baseVO.getPageRow()==null?10:baseVO.getPageRow();
            baseVO.setOffSet((pageNum - 1) * pageRow);
            baseVO.setPageRow(pageRow == 0 ? defaultPageRow : pageRow);
            baseVO.setPageNum(pageNum == 0 ? 1 : pageNum);
        }else{
            logger.info("分页实体对象有误");
            throw new BizException("分页对象有误");
        }
    }

    protected void pageHelper(final Object object) {
        pageHelper(object,10);
    }

    /**
     * 自定义分页返回值
     */

    protected ResultModel successPage(final Object object, List<?> list, int totalCount, Object obj){
        if(object instanceof PageHelpModel){
            PageHelpModel baseVO = (PageHelpModel)object;
            int pageRow = baseVO.getPageRow();
            int totalPage = getPageCounts(pageRow, totalCount);
            int current = baseVO.getPageNum();
            return new ResultModel(0,"SUCCESS",new PageHelpResult(list,totalCount,totalPage,current,obj));
        }else{
            logger.info("分页实体对象有误");
            throw new BizException("分页实体类对象有误");
        }
    }

    protected ResultModel successPage(final Object object, List<?> list, int totalCount){
        return successPage(object,list,totalCount,null);
    }

    protected int getPageCounts(int pageRow, int totalCount) {
        if (totalCount == 0) return 1;
        return totalCount % pageRow > 0 ? totalCount / pageRow + 1 : totalCount / pageRow;
    }
}
